package com.wxiao.exam.Impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wxiao.exam.GradeService;
import com.wxiao.exam.PageBean;
import com.wxiao.exam.Result;
import com.wxiao.exam.mapper.GradeMapper;
import com.wxiao.exam.pojo.GradeDO;
import com.wxiao.exam.pojo.vo.GradeVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <br/>
 * Created by wangxiao on 2019/07/05
 */
@Service("gradeService")
public class GradeServiceImpl implements GradeService {

    private final static Logger LOG = LoggerFactory.getLogger(GradeServiceImpl.class);

    @Resource
    private GradeMapper gradeMapper;

    @Override
    public Result addGrade(GradeVO gradeVO) throws Exception {
        if (findGradeByName(gradeVO)) {
            return Result.getFailure("此年级存在");
        }
        //保存到数据库
        int count = gradeMapper.addGrade(gradeVO);
        if (count > 0) {
            return new Result(Result.CODE_SUCCESS, "添加年级成功");
        }
        throw new RuntimeException("添加错误");
    }

    /**
     * 根据年级名称查询年级是否存在
     *
     * @param gradeVO 年级信息
     * @return true 存在， false 不存在
     */
    public boolean findGradeByName(GradeVO gradeVO) {
        List<GradeDO> gradeDOS = gradeMapper.listGrade(gradeVO);
        if (gradeDOS != null && !gradeDOS.isEmpty()) {
            return true;//存在
        }
        return false;
    }


    @Override
    public Result updateGrade(GradeVO gradeVO) throws Exception {
        return null;
    }

    @Override
    public Result findDetailGrade(GradeVO gradeVO) throws Exception {
        return null;
    }

    @Override
    public PageBean<GradeDO> listGrade(GradeVO query) throws Exception {
        //页码越界
        //获取数据的总个数
        int count = gradeMapper.countGrade(query);
        //计算最大页数
        int max = (count + query.getPageSize() - 1) / query.getPageSize();

        //如果没有数据，那么就返回null，防止重定向过多
        if (max == 0) {
            return null;
        }

	/*	if (query.getPageCode() > max) {
//			query.setPageCode(max);
			throw new PageCodeException("页码越界啦", 1);
		}*/

        PageHelper.startPage(query.getPageCode(), query.getPageSize());
        //CTRL + ALT + 鼠标左    键进入方法的实现
        List<GradeDO> grades = gradeMapper.listGrade(query);
        //插件提供的分页信息
        PageInfo<GradeDO> info = new PageInfo<>(grades);
        //根据插件提供的分页信息，构造我们自己的分页信息
        PageBean<GradeDO> pageBean = new PageBean<GradeDO>(grades, info.getPageSize()
                , query.getPageCode(), (int) info.getTotal(), query.getSize());
        System.out.println(grades);
        return pageBean;
    }

    @Override
    public Result listGradePage(GradeVO gradeVO) throws Exception {
        return null;
    }

    @Override
    public Result countGrade(GradeVO gradeVO) throws Exception {
        return null;
    }

    @Override
    public Result deleteGrade(GradeVO gradeVO) throws Exception {
        return null;
    }

    @Override
    public List<GradeDO> listAll() {
        return gradeMapper.listGrade(new GradeVO());
    }

}